package com.sc.web.gwt.client;

import com.google.gwt.activity.shared.ActivityManager;
import com.google.gwt.activity.shared.ActivityMapper;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.shared.EventBus;
import com.google.gwt.place.shared.Place;
import com.google.gwt.place.shared.PlaceController;
import com.google.gwt.place.shared.PlaceHistoryHandler;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.sc.web.gwt.client.event.LoginEvent;
import com.sc.web.gwt.client.event.LoginEventHandler;
import com.sc.web.gwt.client.place.HomeHistoryMapper;
import com.sc.web.gwt.client.place.LoginWidgetPlace;
import com.sc.web.gwt.client.presenter.mapper.HomeActivityMapper;

/**
 * Created by KienCT
 * Date: 29-05-2011
 * Time: 18:44:11
 */
public class Homepage implements EntryPoint {
    private Place defaultPlace = new LoginWidgetPlace();
    private SimplePanel appWidget = new SimplePanel();

    public void onModuleLoad() {
        final ClientFactory clientFactory = GWT.create(ClientFactory.class);
        EventBus eventBus = clientFactory.getEventBus();
        PlaceController placeController = clientFactory.getPlaceController();

        eventBus.addHandler(LoginEvent.TYPE, new LoginEventHandler() {
            public void onLogin(Object account, LoginEvent event) {
                clientFactory.getAdministratorWidgetView().setWelcomeText(
                        "Administration function is located on the left panel." +
                                "To change your account information, please click on account management." +
                                "Thanks for using our service.");
//                if (userType == 1) {
//                    clientFactory.getAdministratorWidgetView().initAdminFunction();
//                } else if (userType == 2) {
//                    clientFactory.getAdministratorWidgetView().initAgentAdminFunction();
//                }
            }
        });

        // Start ActivityManager for the main widget with our ActivityMapper
        ActivityMapper activityMapper = new HomeActivityMapper(clientFactory);
        ActivityManager activityManager = new ActivityManager(activityMapper, eventBus);
        activityManager.setDisplay(appWidget);

        // Start PlaceHistoryHandler with our PlaceHistoryMapper
        HomeHistoryMapper historyMapper = GWT.create(HomeHistoryMapper.class);
        PlaceHistoryHandler historyHandler = new PlaceHistoryHandler(historyMapper);
        historyHandler.register(placeController, eventBus, defaultPlace);

        RootPanel.get().add(appWidget);
        // Goes to the place represented on URL else default place
        historyHandler.handleCurrentHistory();
    }
}
